
using Youz.Common.Entity;
using Youz.Common.Helper;
using Youz.CreditRepay.DAL;
using Youz.CreditRepay.Entity;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;

namespace Youz.CreditRepay.BLL
{
    /// <summary>
    /// CR_RepaymentPlan BLL Class
    /// </summary>
    public partial class CR_RepaymentPlanBLL : BaseBLL
    {
        CR_RepaymentPlanDAL dal;

        public CR_RepaymentPlanBLL()
        {
            dal = new CR_RepaymentPlanDAL();
            base.ModuleName = "CR_RepaymentPlan";
            base.MenuName = "CR_RepaymentPlan";
        }

        #region  成员方法
        /// <summary>
        ///  新增一条数据
        /// </summary>
        public bool Add(CR_RepaymentPlan model)
        {
            var r = dal.Add(model);

            base.WriteOperationLog(OperationType.Add, r, Core.GetJsonByObjectHaveTime(model));

            return r;
        }

        /// <summary>
        ///  保存一条数据
        /// </summary>
        public bool Update(CR_RepaymentPlan model)
        {
            var r = dal.Update(model);

            base.WriteOperationLog(OperationType.Update, r, Core.GetJsonByObjectHaveTime(model));

            return r;
        }

        /// <summary>
        /// 删除一条数据
        ///</summary>
        public bool Delete(Int32 ID)
        {
            var r = dal.Delete(ID);

            base.WriteOperationLog(OperationType.Delete, r, ID);

            return r;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CR_RepaymentPlan Get(Int32 ID)
        {
            return dal.Get(ID);
        }

        public CR_RepaymentPlan Get(string memberAccount, Int32 ccID)
        {
            return dal.Get(memberAccount, ccID);
        }

        /// <summary>
        /// 得到一个JSON对象
        /// </summary>
        public string GetJson(Int32 ID)
        {
            return Core.GetJsonByObject(Get(ID));
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet Gets(string sqlWhere)
        {
            return dal.Gets(sqlWhere);
        }

        public DataTable GetsByMember(string memberAccount, int cardID)
        {
            return dal.GetsByMember(memberAccount, cardID);
        }

        public CR_RepaymentPlan GetByCardID(int cardID)
        {
            var ds = Gets(string.Format("BankCardID={0} and ([Status]={1} or [Status]={2})", cardID, (int)RepaymentPlanStatus.Temp, (int)RepaymentPlanStatus.Unpay));
            var list = new EntityHelper<CR_RepaymentPlan>().FillEntity(ds);
            return list == null || list.Count == 0 ? null : list[0];
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet Gets(int top, string sqlWhere, string filedOrder)
        {
            return dal.Gets(top, sqlWhere, filedOrder);
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string sqlWhere)
        {
            return dal.GetRecordCount(sqlWhere);
        }

        /// <summary>
        /// 获得数据列表分页
        /// </summary>
        public DataTable Gets(string sqlWhere, int pageindex, int pagesize, out int total, params SqlParameter[] cmdParms)
        {
            return dal.Gets(sqlWhere, pageindex, pagesize, out total, cmdParms);
        }
        #endregion  成员方法

        /// <summary>
        /// 保存计划
        /// </summary>
        /// <param name="cardID"></param>
        /// <param name="billingAmount"></param>
        /// <param name="repaymentAmount"></param>
        /// <param name="fee"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public bool Save(string memberAccount, int cardID, decimal billingAmount, decimal repaymentAmount, decimal fee, List<PlanRepayment> list)
        {
            if (list.Count == 0)
            {
                return false;
            }

            CR_RepaymentPlan plan = GetByCardID(cardID);

            if (plan == null)
            {
                plan = new CR_RepaymentPlan();
            }

            plan.MemberAccount = memberAccount;
            plan.BankCardID = cardID;
            plan.BillingAmount = billingAmount;
            plan.RepaymentAmount = repaymentAmount;
            plan.Fee = fee;
            plan.BeginDate = list.OrderBy(m=>m.ActionTime).First().ActionTime;
            plan.EndDate = list.OrderBy(m => m.ActionTime).Last().ActionTime;
            plan.Quantity = list.Count;

            if (plan.ID == 0)
            {
                if (Add(plan))
                {
                    plan = GetByCardID(cardID);
                }
            }
            else
            {
                Update(plan);
            }

            var bllRepaymentPlanItem = new CR_RepaymentPlanItemBLL();

            //先删除计划项
            bllRepaymentPlanItem.DeleteByPlanID(plan.ID);

            //再添加新项
            var dt = CommonHelper.GetDateTable(bllRepaymentPlanItem.Gets("1=2"));
            var i = 1;
            var j = 1;
            var parentID = 0;
            DataRow row, row1;
            foreach (var repay in list)
            {
                row = dt.NewRow();
                row["ItemID"] = i;
                row["BankCardID"] = cardID;
                row["PlanID"] = plan.ID;
                row["ParentID"] = 0;
                row["Amount"] = repay.Amount;
                row["Date"] = repay.ActionTime;
                row["Status"] = 0;
                row["CreatedOn"] = DateTime.Now;
                row["ModifiedOn"] = DateTime.Now;
                dt.Rows.Add(row);
                parentID = i;
                j = 0;
                foreach (var pay in repay.Pays)
                {
                    row1 = dt.NewRow();
                    row1["ItemID"] = j;
                    row1["BankCardID"] = cardID;
                    row1["PlanID"] = plan.ID;
                    row1["ParentID"] = parentID;
                    row1["Amount"] = pay.Amount;
                    row1["Date"] = pay.ActionTime;
                    row1["Status"] = 0;
                    row1["CreatedOn"] = DateTime.Now;
                    row1["ModifiedOn"] = DateTime.Now;
                    dt.Rows.Add(row1);
                    j++;
                }

                i++;
            }
            dal.Save(dt);

            return true;
        }

        /// <summary>
        /// 获得用户的银行卡还款统计信息
        /// </summary>
        /// <param name="memberAccount"></param>
        /// <param name="cardID"></param>
        /// <returns></returns>
        public DataRow GetSummary(string memberAccount, int cardID)
        {
            return dal.GetSummary(memberAccount, cardID);
        }

        public bool UpdateStatus(int id, RepaymentPlanStatus status)
        {
            return dal.UpdateStatus(id, status);
        }
    }
}


